package com.chaos.system.service;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.chaos.system.api.data.RbacUserRoleData;
import com.chaos.system.entity.RbacUserRole;
import com.chaos.system.mapper.RbacUserRoleMapper;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Jacky
 * @since 2024-05-07
 */
@Slf4j
@Component
public class RbacUserRoleService extends ServiceImpl<RbacUserRoleMapper, RbacUserRole> {

    public ID insertModel(RbacUserRoleData data) {
        RbacUserRole entity = ConvertHelper.convert(data, RbacUserRole.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(RbacUserRoleData data) {
        RbacUserRole entity = ConvertHelper.convert(data, RbacUserRole.class);
        return update(entity, new UpdateWrapper<RbacUserRole>().eq(Table.ID, data.getId()));
    }

    public RbacUserRoleData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<RbacUserRole>().eq(Table.ID, data.getId())), RbacUserRoleData.class);
    }

    public List<RbacUserRoleData> selectByData(RbacUserRoleData data) {
        QueryWrapper<RbacUserRole> query = new QueryWrapper<>();
        query.lambda().eq(!ObjectUtil.isEmpty(data.getUserId()), RbacUserRole::getUserId, data.getUserId());
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), RbacUserRoleData.class);
    }

    public PageList<RbacUserRoleData> selectByPage(PageQueryDto<RbacUserRoleData> pageData) {
        QueryWrapper<RbacUserRole> query = new QueryWrapper<>();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), RbacUserRoleData.class);
    }

    public Set<Long> selectRolesByUserId(Long userId, Long tenantId) {
        return baseMapper.selectRolesByUserId(userId, tenantId);
    }

    public Map<Long, List<Long>> selectByAdminIds(List<Long> adminIds) {
        if (adminIds == null || adminIds.isEmpty()) {
            return Maps.newHashMap();
        }

        LambdaQueryWrapper<RbacUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RbacUserRole::getUserId, adminIds);
        List<RbacUserRole> userRoles = list(queryWrapper);

        return userRoles.stream().collect(Collectors.groupingBy(RbacUserRole::getUserId, Collectors.mapping(RbacUserRole::getRoleId, Collectors.toList())));
    }
}
